package com.zw;


import org.junit.Test;

import java.util.random.RandomGenerator;
import java.util.random.RandomGeneratorFactory;

public class TestJDK17 {


    /**
     * 增强伪随机数生成器
     */
    @Test
    public void test01() {
        RandomGeneratorFactory<RandomGenerator> l64X128 = RandomGeneratorFactory.of("L128X256MixRandom");
        l64X128.create(System.currentTimeMillis())
                .ints(10, 0, 10)
                .forEach(System.out::println);
        System.out.println("##################");
        RandomGenerator randomGenerator = l64X128.create(System.currentTimeMillis());
        int i = randomGenerator.nextInt(5);
        System.out.println(i);
    }

    /**
     * 验证jdk17 switch新特性
     */
    @Test
    public void test02(){
        String s = "1";
        System.out.println(formatterOld(s));
        System.out.println(formatter(s));

        System.out.println(testFooBar(null));
    }

    /**
     * switch jdk17之前的 的特性
     */
    public String formatterOld(Object o) {
        String formatted = "unknown";
        if (o instanceof Integer) {
            int i = (Integer) o;
            formatted = String.format("int %d", i);
        } else if (o instanceof Long) {
            long l = (Long) o;
            formatted = String.format("long %d", l);
        } else if (o instanceof Double) {
            double d = (Double) o;
            formatted = String.format("double %f", d);
        } else if (o instanceof String) {
            String s = (String) o;
            formatted = String.format("String %s", s);
        }
        return formatted;
    }
    /**
     * switch jdk17 的特性
     */
    String formatter(Object o) {
        String formatted = "unknown";
        if (o instanceof Integer i) {
            formatted = String.format("int %d", i);
        } else if (o instanceof Long l) {
            formatted = String.format("long %d", l);
        } else if (o instanceof Double d) {
            formatted = String.format("double %f", d);
        } else if (o instanceof String s) {
            formatted = String.format("String %s", s);
        }
        return formatted;
    }

    String testFooBar(String s) {
        if (s == null){
            return "oops";
        }
        switch (s) {
            case "foo" -> {
                System.out.println("foo");
                return "foo";
            }
            default -> {
                System.out.println("default");
                return "default";
            }
        }
    }

    String testFooBarNew(String s) {
        switch (s) {
            // jdk 21 新特性
//            case null -> {
//                System.out.println("oops");
//                return "oops";
//            }
            case "foo" -> {
                System.out.println("foo");
                return "foo";
            }
            default -> {
                System.out.println("default");
                return "default";
            }
        }
    }

    // jdk 21
//    String formatterNew(Object o){
//        return switch (o) {
//            case Integer i -> String.format("int %d", i);
//            case Long l -> String.format("long %d", l);
//            case Double d -> String.format("double %f", d);
//            case String s -> String.format("String %s", s);
//            default -> "unknown";
//        };
//    }


}
